package com.cy.ppj.service.statistics.impl;

import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.dao.mapper.account.UserOrderInfoMapper;
import com.cy.ppj.dao.mapper.statistics.StatisticsLoanAmountMapper;
import com.cy.ppj.dao.mapper.statistics.StatisticsUserLoanAmountMonthMapper;
import com.cy.ppj.dao.mapper.statistics.StatisticsUserLoanAmountQuarterMapper;
import com.cy.ppj.dao.mapper.statistics.StatisticsUserMapper;
import com.cy.ppj.model.domian.account.UserOrderInfo;
import com.cy.ppj.model.domian.statistics.StatisticsUser;
import com.cy.ppj.model.domian.statistics.StatisticsUserLoanAmountMonth;
import com.cy.ppj.model.domian.statistics.StatisticsUserLoanAmountQuarter;
import com.cy.ppj.model.dto.statistics.StatisticsUserLoanAmountDTO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.statistics.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @description: 统计
 * @author: Ocean
 * @create: 2018-09-03 17:20
 **/
@Service
@Slf4j
public class StatisticsServiceImpl extends BaseService implements StatisticsService {

    @Autowired
    private StatisticsUserMapper statisticsUserMapper;
    @Autowired
    private StatisticsUserLoanAmountMonthMapper statisticsUserLoanAmountMonthMapper;
    @Autowired
    private StatisticsUserLoanAmountQuarterMapper statisticsUserLoanAmountQuarterMapper;
    @Autowired
    private UserOrderInfoMapper userOrderInfoMapper;
    @Autowired
    private StatisticsLoanAmountMapper statisticsLoanAmountMapper;

    @Override
    @Transactional
    public void statisticsUser(Date date) {
        Date startTime = CommonUtils.getDayStartTime(date);
        Date endTime = CommonUtils.getDayEndTime(date);
        int count = statisticsUserMapper.statisticsUser(startTime, endTime);

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        Date time = new Date();

        StatisticsUser statisticsUser = new StatisticsUser();
        statisticsUser.setYear(year);
        statisticsUser.setMonth(month);
        statisticsUser.setDay(day);
        String dateStr = year + "-" + month + "-" + day;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
			statisticsUser.setDate(sdf.parse(dateStr));
		} catch (ParseException e) {
			e.printStackTrace();
		}

        StatisticsUser dbStatisticsUser = statisticsUserMapper.selectOne(statisticsUser);

        statisticsUser.setUpdateTime(time);
        statisticsUser.setCount(count);

        if (dbStatisticsUser == null) {
            statisticsUser.setCreateTime(time);
            statisticsUserMapper.insertSelective(statisticsUser);
        } else {
            statisticsUser.setId(dbStatisticsUser.getId());
            statisticsUserMapper.updateByPrimaryKeySelective(statisticsUser);
        }
    }

    @Override
    @Transactional
    public void addStatisticsUserLoanAmount(StatisticsUserLoanAmountDTO statisticsUserLoanAmount) {

        Long userId = statisticsUserLoanAmount.getUserId();
        BigDecimal loanAmount = statisticsUserLoanAmount.getLoanAmount();
        Date time = statisticsUserLoanAmount.getTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int quarter = CommonUtils.getQuarter(month);

        //月度统计
        StatisticsUserLoanAmountMonth monthDO = new StatisticsUserLoanAmountMonth();
        monthDO.setUserId(userId);
        monthDO.setLoanAmount(loanAmount);
        monthDO.setYear(year);
        monthDO.setMonth(month);
        monthDO.setCreateTime(time);
        statisticsUserLoanAmountMonthMapper.insertStatisticsUserLoanAmountMonth(monthDO);

        //季度统计
        StatisticsUserLoanAmountQuarter quarterDO = new StatisticsUserLoanAmountQuarter();
        quarterDO.setUserId(userId);
        quarterDO.setLoanAmount(loanAmount);
        quarterDO.setYear(year);
        quarterDO.setQuarter(quarter);
        quarterDO.setCreateTime(time);
        statisticsUserLoanAmountQuarterMapper.insertStatisticsUserLoanAmountQuarter(quarterDO);

        //维护用户数据
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        userOrderInfo.setUserId(userId);
        userOrderInfo.setLoanAmount(loanAmount);
        userOrderInfo.setOrderLoanCount(1);
        userOrderInfo.setOrderCount(0);
        userOrderInfo.setCreateTime(time);
        userOrderInfoMapper.addUserOrderInfo(userOrderInfo);
    }

    @Override
    public void addStatisticsLoanAmount(Date startTime, Date endTime) {
        Date createTime = new Date();
        statisticsLoanAmountMapper.isnertStatisticsLoanAmount(createTime, startTime, endTime);
    }

}
